package JUC.Lock;

import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.StampedLock;

/**
 * StampedLock  中断会导致CPU飙升
 * @author CKC
 * @date 2022/11/29 19:00
 */
public class TestStampedLock {
    public static void main(String[] args) throws InterruptedException {
        final StampedLock lock = new StampedLock();
        new Thread(() -> {
            long readLong = lock.writeLock();
            try {
                LockSupport.parkNanos(600000000000L);
            } finally {
                lock.unlockWrite(readLong);
            }
        }).start();
        Thread.sleep(100);
        for (int i = 0; i < 3; ++i) {
            new Thread(() -> {
                Thread.currentThread().interrupt();
                long lockr = lock.readLock();
                System.out.println(Thread.currentThread().getName() + " get read lock");
                lock.unlockRead(lockr);
            }).start();
        }
    }
}
